package com.jiangxiaofei.citycloud.activity.recruit;

import android.os.Bundle;
import android.support.v7.widget.RecyclerView;
import android.widget.Toast;

import com.blankj.utilcode.util.EmptyUtils;
import com.jiangxiaofei.citycloud.R;
import com.jiangxiaofei.citycloud.adapter.RetruitWorkListAdapter;
import com.jiangxiaofei.citycloud.base.BaseActivity;
import com.jiangxiaofei.citycloud.bean.JobListBean;
import com.jiangxiaofei.citycloud.constants.Constants;
import com.jiangxiaofei.citycloud.event.EventMessage;
import com.jiangxiaofei.citycloud.event.RefreshMessage;
import com.jiangxiaofei.citycloud.mvp.RecruitConstant;
import com.jiangxiaofei.citycloud.mvp.RecruitPersenter;
import com.jiangxiaofei.citycloud.view.FullyLinearLayoutManager;
import com.jiangxiaofei.citycloud.view.IOSAlertDialog;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.List;

import butterknife.ButterKnife;
import butterknife.InjectView;

/**
 * Created by Administrator on 2017/9/8 0008.
 * 招聘信息列表详情页
 */

public class RecruitFindWorkListActivity extends BaseActivity implements
        RecruitConstant.GetJobListView ,RecruitConstant.DeleteWorkView{
    @InjectView(R.id.find_work_recy_list)
    RecyclerView findWorkRecyList;
    private RetruitWorkListAdapter adapter;
    RecruitConstant.GetJobListPersenter getJobListPersenter;
    RecruitConstant.DeleteWorkPersenter deleteWorkPersenter;
    private List<JobListBean> jobList;
    private String deleteId;

    @Override
    protected void initView(Bundle savedInstanceState) {
        setContentView(R.layout.activity_recruit_find_work_list_layout);
        mActivity = this;
        mContext = this;
        ButterKnife.inject(this);
        initTitle();
        initDate();
        getJobListPersenter = new RecruitPersenter(mContext, (RecruitConstant.GetJobListView) this);
        deleteWorkPersenter=new RecruitPersenter(mContext, (RecruitConstant.DeleteWorkView) mActivity);
        getJobListPersenter.getJobList();
        EventBus.getDefault().register(this);
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void Refresh(RefreshMessage message) {
        getJobListPersenter.getJobList();
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void delete(EventMessage.DoWorkLongDeleteMsg longDelete) {
        if (EmptyUtils.isNotEmpty(longDelete.getId())) {
            deleteId=longDelete.getId();
            showDeleteDialog();
        }
    }

    private void showDeleteDialog() {
        IOSAlertDialog dialog = new IOSAlertDialog(mContext);
        dialog.builder()
                .setTitle("提示")
                .setMsg("是否删除此条信息？")
                .setPositiveButton("确定", view -> deleteWorkPersenter.DeleteWork())
                .setNegativeButton("取消", view -> {}).show();
    }

    private void initDate() {
        jobList = new ArrayList<>();
        adapter = new RetruitWorkListAdapter(jobList, mContext);
        findWorkRecyList.setLayoutManager(new FullyLinearLayoutManager(mContext));
        findWorkRecyList.setAdapter(adapter);
    }

    @Override
    protected void setListener() {
        setCTitle("我发布的劳务信息");
        setLeftOneBg(R.drawable.ic_back);
    }

    @Override
    protected void processLogic(Bundle savedInstanceState) {

    }


    @Override
    public void setData(Object o) {
        jobList.clear();
        List<JobListBean> list = (List<JobListBean>) o;
        for (int i = 0; i < list.size(); i++) {
            jobList.add(list.get(i));
        }
        adapter.notifyDataSetChanged();
    }

    @Override
    public void showLoading() {

    }

    @Override
    public void hideLoading() {

    }

    @Override
    public void message(String str) {

    }

    @Override
    public int getJobList() {
        return Constants.userId;
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

    @Override
    public String deleteWorkId() {
        return deleteId;
    }

    @Override
    public void onDeleteSuccess() {
        Toast.makeText(mContext, "删除成功", Toast.LENGTH_SHORT).show();
        getJobListPersenter.getJobList();
    }
}
